<template>
	<view style="background-color: var(--view-theme);height: 100vh;" :style="viewColor">
		<view class="system-height" :style="{ height: statusBarHeight }"></view>
		<view class="title-bar" style="height: 43px;">
			<view class="icon" @click="close" v-if="!isHome">
				<image src="../static/angle-left.png"></image>
			</view>
			<view class="icon" @click="close" v-else>
				<image src="../static/home.png"></image>
			</view>
			账户登录
		</view>
		<view class="wechat_login">
			<view class="img">
				<image src="../static/wechat_login.png" mode="widthFix"></image>
			</view>
			<view class="odictwo">
				<div class='twimg'>
					<image :src="loginLogo" mode=""></image>
				</div>
				<view style="padding-top: 30rpx"></view>
				<div class="twtext">
					您好，请先登录再进行此操作!
				</div>
				<div class="annui">
					<button hover-class="none" @click='close' class="bg-red btn1">随便逛逛</button>
					<!-- #ifdef H5 || APP-PLUS -->
					<button class="item grant" @tap="toWecahtAuth">去授权</button>
					<!-- #endif -->
					<!-- #ifdef  MP -->
					<button class="item grant" type="primary" open-type="getUserInfo" lang="zh_CN" @tap="getUserProfile">快捷登录</button>
					<!-- #endif -->
				</div>
			</view>
		</view>
		<block v-if="isPhoneBox">
			<routinePhone :logoUrl="logoUrl" :isPhoneBox="isPhoneBox" @close="bindPhoneClose" :authKey="authKey" :viewColor="viewColor">
			</routinePhone>
		</block>
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
	// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
	// +----------------------------------------------------------------------
	// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
	// +----------------------------------------------------------------------
	// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
	// +----------------------------------------------------------------------
	// | Author: CRMEB Team <admin@crmeb.com>
	// +----------------------------------------------------------------------
	var statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	const app = getApp();
	import Cache from '@/utils/cache';
	import {
		getLogo, commonAuth,getUserPhone
	} from '@/api/public.js';
	import { LOGO_URL, USER_INFO, EXPIRES_TIME} from '@/config/cache';
	import { mapGetters } from 'vuex';
	import Routine from '@/libs/routine';
	import { configMap } from '@/utils/index';
	import Auth from '@/libs/wechat';
	import { toLogin } from '@/libs/login';
	import routinePhone from '../components/login_mobile/routine_phone.vue';

	export default {
		name: 'Authorize',
		props: {
			isAuto: {
				type: Boolean,
				default: true
			},
			isGoIndex: {
				type: Boolean,
				default: true
			},
			isShowAuth: {
				type: Boolean,
				default: false
			}
		},
		components: {
			routinePhone
		},
		data() {
			return {
				statusBarHeight: statusBarHeight,
				canUseGetUserProfile: false,
				code: null,
				isPhoneBox:false,
				authKey: '',
				loginLogo: '',
			}
		},
		computed: {
			...mapGetters(['isLogin', 'userInfo', 'viewColor']),
			...configMap(['routine_logo','login_logo'])
		},
		watch: {
			isLogin(n) {
				n === true && this.$emit('onLoadFun', this.userInfo);
			},
			isShowAuth(n) {
				this.getCode(this.isShowAuth)
			}
		},
		created() {
			if (wx.getUserProfile) {
				this.canUseGetUserProfile = true
			}
			this.setAuthStatus();
			this.getCode(true)
			// #ifdef MP
			this.loginLogo = this.routine_logo
			// #endif
			// #ifndef MP
			this.loginLogo = this.login_logo
			// #endif
		},
		methods: {
			bindPhoneClose(data) {
				if (data.isStatus) {
					this.isPhoneBox = false;
					this.$util.Tips({
						title: `登录成功`,
						icon: 'success'
					}, {
						tab: 3
					});
				} else {
					this.isPhoneBox = false;
				}
			},
			setAuthStatus() {
				//#ifdef MP
				Routine.authorize().then(res => {
					if (res.islogin === false)
						this.$emit('onLoadFun', this.userInfo);
				}).catch(res => {
					if (this.isAuto)
						this.$emit('authColse', true);
				})
				//#endif
			},
			getCode(n){
				// #ifdef MP
					if (n) {
						uni.showLoading({
							title: '正在登录中'
						});
						Routine.getCode().then(code => {
							uni.hideLoading();
							this.code = code;
						}).catch(e => {
							uni.hideLoading();
							uni.showToast({
								title: '登录失败',
								duration: 2000
							});
						})
					} else {
						this.code = null;
					}
					// #endif
					// #ifndef MP
					if(n){
						this.code = 1;
					}
					// #endif
			},
			toWecahtAuth(){
				toLogin(true);
			},
			getUserProfile(e) {
				let self = this;
				Routine.getUserProfile()
					.then(res => {
						let userInfo = res.userInfo;
						userInfo.code = this.code;
						userInfo.spread = app.globalData.spid; //获取推广人ID
						userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
						commonAuth({
							auth: {
							type:'routine',
							auth: userInfo
						}
						}).then(res=>{
							if(res.data.status == 200){
								let time = res.data.result.expires_time - Cache.time();
								self.$store.commit('UPDATE_USERINFO', res.data.result.user);
								self.$store.commit('LOGIN', {token:res.data.result.token, time:time});
								self.$store.commit('SETUID', res.data.result.user.uid);
								uni.setStorageSync('UID',res.data.result.user.uid)
								Cache.set(EXPIRES_TIME,res.data.result.expires_time,time);
								Cache.set(USER_INFO,res.data.result.user,time);
								uni.navigateBack()
								this.$emit('onLoadFun', res.data.result.user);
								// uni.redirectTo({
								// 	url:"/pages/index/minp?share_uid="+res.data.result.user.uid
								// })
							}else{
								uni.setStorageSync('auth_token',res.data.result.key);
								// 显示微信授权弹窗
								self.isPhoneBox=true
								// return uni.navigateTo({
								// 	url:'/pages/users/login/index'
								// })
								// Cache.set('login_back_url', '/pages/index/minp');
							}
						}).catch(res => {
							uni.hideLoading();
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000,
								
							});
						});
					})
					.catch(res => {
						uni.hideLoading();
					});
	
			},
			close() {
				uni.navigateBack()
			},
		}
	}
</script>

<style scoped lang='scss'>
	page {
		background: #fff;
	}
	
	.wechat_login {
		padding: 72rpx 34rpx;
	
		.img image {
			width: 100%;
		}
	
		.odictwo {
			width: 650rpx;
			margin-left: 16rpx;
			background-color: #fff;
			border-radius: 25rpx;
			position: relative;
	
			.twimg {
				position: absolute;
				top: -50rpx;
				left: 50rpx;
				width: 120rpx;
				height: 120rpx;
				border: 2rpx solid #fff;
				border-radius: 100rpx;
				overflow: hidden;
	
				image {
					width: 100%;
					height: 100%;
				}
			}
	
			.twtext {
				margin: 0rpx 48rpx;
				margin-top: 70rpx;
				background-color: #f7f8fa;
				padding: 36rpx 24rpx;
			}
	
			.annui {
				width: 100%;
				display: flex;
				justify-content: space-between;
				padding: 50rpx 50rpx 0 50rpx;
	
				button {
					width: 40%;
					height: 86rpx;
					line-height: 86rpx;
					margin-bottom: 40rpx;
					border-radius: 120rpx;
					font-size: 30rpx;
	
					&.btn1 {
						background-color: var(--view-theme);
						color: #fff;
					}
	
					&.btn2 {
						border: 2rpx solid var(--view-theme);
						color: var(--view-theme);
					}
				}
			}
		}
	
		.btn-wrapper {
			margin-top: 86rpx;
			padding: 0 66rpx;
	
			button {
				width: 100%;
				height: 86rpx;
				line-height: 86rpx;
				margin-bottom: 40rpx;
				border-radius: 120rpx;
				font-size: 30rpx;
	
				&.btn1 {
					color: #fff;
				}
	
				&.btn2 {
					color: #666666;
					border: 1px solid #666666;
				}
			}
	
		}
	}
	
	.title-bar {
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 36rpx;
		color: #fff;
	}
	
	.icon {
		position: absolute;
		left: 30rpx;
		top: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 86rpx;
		height: 86rpx;
	
		image {
			width: 38rpx;
			height: 38rpx;
		}
	}
</style>
